package org.qq.ui;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import org.qq.clientthread.ClientLoginThread;
import org.qq.util.Util;
/**
 * 
 * project_name:QQ
 * package_name:org.qq.register
 * file_name:Register.java
 * @author cui
 * create_date:2015年8月27日 下午5:42:00
 * modified_date:
 * description:登录界面，（登录进入主界面，注册进入注册界面）
 *
 */
public class Login extends JFrame{
	private JTextField loginNameField;				//用户名文本框
	private JLabel welcomLabel;						//欢迎标签
	private JPasswordField  loginPasswordField;		//密码文本框
	private JLabel loginNameLabel;					//用户名标签
	private JLabel loginPasswordLabel;				//密码标签
	private JButton loginButton;					//登录按钮
	private JButton registerButton;					//注册按钮
	private JTextField ipField;						//ip文本框
	private JLabel portLabel;						//端口标签
	private JTextField portField;					//端口文本框
	private JLabel ipLabel;							//ip标签
	private String serverIp;
	private Integer serverPort;
	
	
	public String getServerIp() {
		return serverIp;
	}
	public void setServerIp(String serverIp) {
		this.serverIp = serverIp;
	}
	public Integer getServerPort() {
		return serverPort;
	}
	public void setServerPort(Integer serverPort) {
		this.serverPort = serverPort;
	}
	public Login() {
		setBackground(Color.GRAY);
		getContentPane().setBackground(Color.PINK);
		this.setBounds(0, 0, 378, 373);
		this.setTitle("QQ登录");
		Util.screen(this);
		this.setDefaultCloseOperation(3);
		this.setResizable(false); 														// 不可改变大小
		getContentPane().setLayout(null); 												// 自定义
		
		loginNameLabel = new JLabel("帐  号:");
		loginNameLabel.setFont(new Font("宋体", Font.PLAIN, 14));
		loginNameLabel.setBackground(Color.GREEN);
		loginNameLabel.setBounds(62, 70, 51, 31);
		getContentPane().add(loginNameLabel);
		
		loginPasswordLabel = new JLabel("密  码:");
		loginPasswordLabel.setFont(new Font("宋体", Font.PLAIN, 14));
		loginPasswordLabel.setBounds(60, 119, 53, 31);
		getContentPane().add(loginPasswordLabel);
		
		loginNameField = new JTextField();
		loginNameField.setFont(new Font("新宋体", Font.PLAIN, 14));
		loginNameField.setBounds(145, 67, 153, 37);
		getContentPane().add(loginNameField);
		loginNameField.setColumns(10);
		
		loginPasswordField = new JPasswordField();
		loginPasswordField.setFont(new Font("新宋体", Font.PLAIN, 14));
		loginPasswordField.setBounds(145, 116, 153, 37);
		getContentPane().add(loginPasswordField);
		loginPasswordField.setColumns(10);

		
		registerButton = new JButton("注册");
		registerButton.setFont(new Font("幼圆", Font.PLAIN, 15));
		registerButton.setBackground(Color.ORANGE);
		registerButton.setBounds(62, 275, 93, 42);
		getContentPane().add(registerButton);
		registerButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				Regist register = new Regist();
				register.setVisible(true);
			}
		});
		
		/**
		 * 登录按钮
		 */
		loginButton = new JButton("登录");
		loginButton.setFont(new Font("幼圆", Font.PLAIN, 15));
		loginButton.setBackground(Color.ORANGE);
		loginButton.setBounds(205, 275, 93, 42);
		getContentPane().add(loginButton);
		loginButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				
				String ipstr = ipField.getText();
				int port = Integer.parseInt(portField.getText());
				Socket s =null;
				try {
					s = new Socket(ipstr,port);
					String str1 = "帐号:" + loginNameField.getText() + "----密码:" + new String(loginPasswordField.getPassword())+"-----login";
					ClientLoginThread clt = new ClientLoginThread(s,Login.this,loginNameField.getText());
					clt.start();
					clt.sendMessageToServer(str1);
				} catch (UnknownHostException e1) {
					e1.printStackTrace();
				} catch (IOException e1) {
					e1.printStackTrace();
				}
			}
		});
		
		welcomLabel = new JLabel("欢迎进入QQ666");
		welcomLabel.setFont(new Font("微软雅黑", Font.PLAIN, 18));
		welcomLabel.setBackground(new Color(255, 160, 122));
		welcomLabel.setForeground(Color.BLACK);
		welcomLabel.setHorizontalAlignment(SwingConstants.CENTER);
		welcomLabel.setBounds(62, 10, 255, 45);
		getContentPane().add(welcomLabel);
		
		ipLabel = new JLabel("IP地址:");
		ipLabel.setFont(new Font("新宋体", Font.PLAIN, 14));
		ipLabel.setBounds(62, 174, 51, 22);
		getContentPane().add(ipLabel);
		
		String ip = null;
		try {
			ip = InetAddress.getLocalHost().getHostAddress();
		} catch (UnknownHostException e1) {
			e1.printStackTrace();
		}
		
		ipField = new JTextField();
		ipField.setFont(new Font("新宋体", Font.PLAIN, 14));
		ipField.setBounds(145, 165, 153, 37);
		getContentPane().add(ipField);
		ipField.setColumns(10);
		ipField.setText(ip);
		
		portLabel = new JLabel("端口号:");
		portLabel.setHorizontalAlignment(SwingConstants.CENTER);
		portLabel.setFont(new Font("新宋体", Font.PLAIN, 14));
		portLabel.setBounds(62, 221, 51, 22);
		getContentPane().add(portLabel);
		
		portField = new JTextField();
		portField.setBounds(145, 214, 153, 37);
		getContentPane().add(portField);
		portField.setColumns(10);
		portField.setText("30000");
		
		setServerIp(ipField.getText());
		setServerPort(Integer.parseInt(portField.getText()));
	}
}

